/* eslint-disable no-unused-vars */
/* eslint-disable no-console */
const TerserPlugin = require('terser-webpack-plugin')
const chalk = require('chalk')
const path = require('path')
const fs = require('fs')
const interfaces = require('os').networkInterfaces()
const packages = require('./package.json')
let IPAdress = '';
for (var devName in interfaces) {
    var iface = interfaces[devName];
    for (var i = 0; i < iface.length; i++) {
        var alias = iface[i];
        if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
            IPAdress = alias.address;
        }
    }
}
const date = new Date()
const time = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' ' + date.getHours()
const outputDir =  path.join(__dirname, 'olap-front/static/')
const variable = fs.readFileSync(path.join(__dirname, 'src/styles/variable.scss'),'utf-8')
const theme = variable.split(';').find(str => str.includes('$theme-color')).split(':')[1].trim()
console.log(
    chalk.green("IPAdress: " + IPAdress),
    chalk.green("\nTheme: " + theme),
)
const pages = {
    index:{
        entry: './src/main.js',
        template: './public/index.html',
        filename: 'index.html',
        title: packages.title,
        theme: theme.substr(1)
    }
}
if(process.env.NODE_ENV === 'development') {
    pages.preview = {
        entry: './preview/main.js',
        template: './public/preview.html',
        filename: 'preview.html',
        title: '数据分析平台预览'
    }
}
module.exports = {
    publicPath: './',
    outputDir: process.env.NODE_ENV === 'node' ? outputDir : `cboard-platform-${time}`,
    pages: pages,
    devServer: {
        compress: true,
        proxy: {
            '/cboard': {
                target: 'http://192.168.1.121',
                changeOrigin: true,
                pathRewrite:{ // 重写地址
                    '^/cboard': ''
                }
            }
        }
    },
    productionSourceMap: false,
    configureWebpack: config => {
        config.externals = {
            'vue': 'Vue',
            'element-ui': 'ElementUI',
            'echarts': 'echarts'
        }
        Object.assign(config.resolve.alias, {
            'views': '@/views',
            'preview':  path.resolve(process.cwd(), 'preview')
        })
        if (process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'node') { 
            config.plugins.push(
                new TerserPlugin({
                    terserOptions: {
                        warnings: false,
                        compress: {
                            drop_debugger: true,
                            drop_console: true,
                            pure_funcs:['console.log']
                        }
                    }
                })
            );
            config.devtool = "source-map";
        } else if(process.env.NODE_ENV === 'development') {
            config.devtool = "source-map";
        }
    },
    chainWebpack: config => {
        if (process.env.NODE_ENV === 'production') { 
            // console.log(config)
        }
    },
    css: {
        extract: true,
        loaderOptions: {
            sass: {
                data: `@import "@/styles/global.scss";`,
                javascriptEnabled: true,
            }
        }
    },
    pwa: {
        name: '数据分析平台',
        themeColor: theme,
        assetsVersion: time
    }
}